import "./Array"
import "./Date"
import "./String"

import forEach from "lodash/forEach"
import filter from "lodash/filter"





(() => {

    /**
     * 根据id子元素添加到父对象下
     *
     * @public
     * @param {array} 原始数组
     * @param {string} 父元素标识
     * @param {string} 子元素标识
     * @param {string} 生成元素下标名
     * @returns {Array} 返回递归后的数组
     */
    var recurrence = function (array,parent="parentId",id="resId",childs="childs") {
        var tree =array||[];
        var groups = {};

        function init(pid) {
            group();
            return getChilds(groups[pid]);
        }
        function group() {
            for(var i=0;i<tree.length;i++){
                if(groups[tree[i][parent]]){
                    groups[tree[i][parent]].push(tree[i]);
                }else{
                    groups[tree[i][parent]]=[];
                    groups[tree[i][parent]].push(tree[i]);
                }
            }
        }
        function getChilds(a) {
            if(!a){return []}
            var childs_array = []
            for(var i=0;i<a.length;i++){
                childs_array.push(a[i])
                a[i][childs] = getChilds(groups[a[i][id]])
            };
            return childs_array;
        }
        return init(0)
    }

    $.extend({
        forEach,
        filter,
        recurrence
    })
})()

